package com.google.gwt.sample.dynatablerf.client.presenters;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.sample.dynatablerf.shared.GrifoRequestFactory;
import com.google.web.bindery.requestfactory.shared.RequestContext;

public abstract class AbstractClosableTabPresenter<T extends RequestContext>
		extends AbstractHasRequestPresenter<T> {

	public interface ClosableTabDisplay extends TabDisplay {

		HasClickHandlers getCloseTabClickHandlers();
	}

	public AbstractClosableTabPresenter(EventBus eventBus,
			GrifoRequestFactory requestFactory) {
		super(eventBus, requestFactory);
	}

	@Override
	protected void bind() {
		getClosableTabDisplay().getCloseTabClickHandlers().addClickHandler(
				new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						getTabLayoutPanel().remove(getDisplay().asWidget());
						getTabLayoutPanel().selectTab(
								getTabLayoutPanel().getWidgetCount() - 1);
						fireCloseWindowEvent();
					}
				});

	}

	protected abstract ClosableTabDisplay getClosableTabDisplay();

	protected abstract void fireCloseWindowEvent();

	@Override
	protected TabDisplay getTabDisplay() {
		return getClosableTabDisplay();
	}
}
